﻿using Mozhi.Abp.Core.Networks;
using Mozhi.Abp.Domain.Repositories;
using Org.BouncyCastle.Crypto;
using System;
using System.Collections.Generic;
using System.Security.Policy;
using System.Threading.Tasks;
using Volo.Abp;

namespace Mozhi.Abp.Infrastructure.RemoteRepositories
{
    public class RemoteRepository<TEntity> : IRemoteRepository<TEntity> where TEntity : class
    {
        protected IRemoteServiceClient _remoteServiceClient;

        protected string _serviceName;

        protected string _url;

        public RemoteRepository(IRemoteServiceClient remoteServiceClient , string serviceName , string url)
        {
            Check.NotNullOrEmpty(serviceName, "serviceName");
            Check.NotNullOrEmpty(url, "url");

            _remoteServiceClient = remoteServiceClient;
            _serviceName = serviceName;
            _url = url.EndsWith("/") ? url : url+"/";


        }

        public virtual async Task<TEntity> GetAsync(Guid id)
        {
            var url =  _url + id;

            var entity = await _remoteServiceClient.GetAsync<TEntity>(_serviceName, url);

            return entity;
        }

        public virtual async Task<List<TEntity>> GetListAsync(List<Guid> ids)
        {
            var url = _url + "?ids=" + string.Join(",", ids);

            var pageResult = await _remoteServiceClient.GetAsync<RemotePageResult<TEntity>>(_serviceName, url);

            return pageResult != null ? pageResult.Items : new List<TEntity>();
        }

        public virtual async Task<TEntity> InsertAsync(TEntity data)
        {
            var url = _url;

            var entity = await _remoteServiceClient.PostAysnc<TEntity>(_serviceName, url, data);

            return entity;
        }

        #region
        #endregion
    }
}
